package com.xiaoshuidi.cloud.module.finance.service.voucher;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.xiaoshuidi.cloud.framework.common.util.date.DateUtils;
import com.xiaoshuidi.cloud.module.finance.kdclient.*;
import lombok.Data;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.client.Stub;
import org.apache.axis.message.SOAPHeaderElement;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.net.URL;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

import static com.xiaoshuidi.cloud.module.finance.util.KingdeeUtil.easLogin;
import static com.xiaoshuidi.cloud.module.finance.util.KingdeeUtil.importVoucher;

/**
 * @author cz
 * @version 1.0
 * @description: TODO
 * @date 2024/4/28 10:29
 */
public class DYTestApi {
    public static void main(String[] args) {


        try {
            WSContext wsContext = easLogin();
            System.out.println("userName："+ wsContext.getUserName()+"；sessionId："+wsContext.getSessionId());
//            System.out.println("EAS context"+wsContext);
           String[] result = importVoucher(wsContext.getSessionId());
            System.out.println(result[4]);
//            importVoucher(null);
//            importVoucher();
        }catch (Exception e){
            e.printStackTrace();
        }
    }



    public static WSContext easLogin(){
        EASLoginProxy proxy = null;
        WSContext context = null;
        try {
            //登录
//            URL loginURL = new URL("http://eas.dongyuansh.com:6888/ormrpc/services/EASLogin?wsdl");
            URL loginURL = new URL("http://eas.dongyuansh.com:6888/ormrpc/services/EASLogin?wsdl");
            proxy = new EASLoginProxyServiceLocator().getEASLogin(loginURL);
            // 1.登录账号； 2.登录密码； 3.默认"eas"; 4.数据中心名称; 5.语言 （l1,l2,l3）一般默认"l2"; 6.数据库类型(MS SQL Server:0,DB2 UDB:1,Oracle:2,MS SQL Server:3)
            context = proxy.login("user", "dongyuan.321", "eas", "dycs", "l2", 0);
//            context = proxy.login("user", "dongyuan.321", "eas", "DYZS", "l2", 0);
        }catch (Exception e){
            e.printStackTrace();
        }
        return context;
    }
    public static String [][] importVoucher() throws ServiceException, RemoteException {
        //调用登陆接口
        Service s=new Service();
        Call call=(Call)s.createCall();
        call.setOperationName("login");
        call.setTargetEndpointAddress("http://eas.dongyuansh.com:6888/ormrpc/services/EASLogin?wsdl");
        call.setReturnType(new QName("urn:client","WSContext"));
        call.setReturnClass(WSContext.class);
        call.setReturnQName(new QName("","loginReturn"));
        //超时
        call.setTimeout(Integer.valueOf(1000*600000*60));
        call.setMaintainSession(true);
        //登陆接口参数
        WSContext rs= (WSContext)call.invoke(new Object[]{"user", "dongyuan.321", "eas", "dyeas2023", "l2",
                Integer.valueOf(0)});

        call.clearOperation();
        //调用业务接口
        call.setOperationName("importVoucherOfReturnID");
        call.setTargetEndpointAddress("http://eas.dongyuansh.com:6888/ormrpc/services/WSWSVoucher?wsdl");
        call.setReturnQName(new QName("","importVoucherOfReturnIDReturn"));
        call.setTimeout(Integer.valueOf(1000*600000*60));
        call.setMaintainSession(true);
        //设置登录返回的session在soap头 "http://login.webservice.bos.kingdee.com"是固定的
        SOAPHeaderElement header=new
                SOAPHeaderElement("http://login.webservice.bos.kingdee.com","SessionId",
                rs.getSessionId());
        call.addHeader(header);
        WSWSVoucher[] wswsVouchers = getImportVoucher();
        //接口参数
        String [][] is =(String [][])call.invoke(new Object[]{wswsVouchers, 0, 0} );
        System.out.println(is[0][0]);
        System.out.println(is[0][1]);
        System.out.println(is[0][2]);
        System.out.println(is[0][3]);
        System.out.println(is[0][4]);
        System.out.println(is[0][5]);
        System.out.println(is[0][6]);
        return is;
    }

    public static WSWSVoucher[] getImportVoucher() {
        WSWSVoucher[] voucher = new WSWSVoucher[3];
        //借方
        WSWSVoucher ss = new WSWSVoucher();
        //凭证头
        ss.setCompanyNumber("DYSH034");
        ss.setBookedDate("2023-12-20");
        ss.setBizDate("2023-12-20");
        ss.setPeriodYear(2023);
        ss.setPeriodNumber(12);
        ss.setVoucherType("转账凭证");
        ss.setVoucherNumber("lc-test002");

        //分录行
        ss.setEntrySeq(2);
        ss.setVoucherAbstract("XX公寓公寓XX月A门店、B门店款项开具发票*租金");
        ss.setAccountNumber("1122.02.06.01");
        ss.setCurrencyNumber("BB01");

        ss.setEntryDC(1);
        ss.setOriginalAmount(8000);
        ss.setDebitAmount(8000);
        ss.setCreator("预设用户");

        // 辅助账
        ss.setAsstSeq(1);
        ss.setAsstActType1("客户");
        ss.setAsstActNumber1("HFLB399");
        ss.setAsstActName1("米乐公寓");
        ss.setAsstSeq(2);
        ss.setAsstActType2("租赁项目");
        ss.setAsstActNumber2("JYSY01");
        ss.setAsstActName2("恒福丽宝广场");
        ss.setAsstSeq(3);
        ss.setAsstActType3("物业地址");
        ss.setAsstActNumber3("JYSY");
        ss.setAsstActName3("景御丽宝广场");
        voucher[0]=ss;

        // 贷方
        WSWSVoucher sss2 = new WSWSVoucher();

        //凭证头
        sss2.setCompanyNumber("DYSH034");
        sss2.setBookedDate("2023-12-20");
        sss2.setBizDate("2023-12-20");
        sss2.setPeriodYear(2023);
        sss2.setPeriodNumber(12);
        sss2.setVoucherType("转账凭证");
        sss2.setVoucherNumber("lc-test002");

        //分录行
        sss2.setEntrySeq(1);
        sss2.setVoucherAbstract("XX公寓公寓XX月A门店、B门店款项开具发票*租金");
        sss2.setAccountNumber("6001.07.01");
        sss2.setCurrencyNumber("BB01");

        sss2.setEntryDC(-1);
        sss2.setOriginalAmount(7280);
        sss2.setCreditAmount(7280);

        // 辅助账
        sss2.setAsstSeq(1);
        sss2.setAsstActType1("客户");
        sss2.setAsstActNumber1("HFLB399");
        sss2.setAsstActName1("米乐公寓");
        sss2.setAsstSeq(2);
        sss2.setAsstActType2("租赁项目");
        sss2.setAsstActNumber2("JYSY01");
        sss2.setAsstActName2("恒福丽宝广场");
        sss2.setAsstSeq(3);
        sss2.setAsstActType3("物业地址");
        sss2.setAsstActNumber3("JYSY");
        sss2.setAsstActName3("景御丽宝广场");
        voucher[1]=sss2;

        //贷方
        WSWSVoucher ss3 = new WSWSVoucher();
        //凭证头
        ss3.setCompanyNumber("DYSH034");
        ss3.setBookedDate("2023-12-20");
        ss3.setBizDate("2023-12-20");
        ss3.setPeriodYear(2023);
        ss3.setPeriodNumber(12);
        ss3.setVoucherType("转账凭证");
        ss3.setVoucherNumber("lc-test002");

        //分录行
        ss3.setEntrySeq(1);
        ss3.setAccountNumber("2221.01.05.03");
        ss3.setVoucherAbstract("XX公寓公寓XX月A门店、B门店款项开具发票*租金");
        ss3.setCurrencyNumber("BB01");
        ss3.setEntryDC(-1);
        ss3.setOriginalAmount(720);
        ss3.setCreditAmount(720);
        ss3.setCreator("预设用户");
        voucher[2]=ss3;
        return voucher;
    }

    public static String[] importVoucher(String sessionId){
        URL portAddress= null;
        WSWSVoucherSrvProxyServiceLocator locator = new WSWSVoucherSrvProxyServiceLocator();
        try{
            WSWSVoucher[] voucher = getImportVoucher();
            portAddress = new URL("http://eas.dongyuansh.com:6888/ormrpc/services/WSWSVoucher?wsdl");
            WSWSVoucherSrvProxy wswsVoucher = locator.getWSWSVoucher(portAddress);
            ((Stub) wswsVoucher).setHeader("http://login.webservice.bos.kingdee.com", "SessionId",
                    sessionId);
            String[][] is = wswsVoucher.importVoucherOfReturnID(voucher, 0, 0);
            System.out.println(is[0][0]);
            System.out.println(is[0][1]);
            System.out.println(is[0][2]);
            System.out.println(is[0][3]);
            System.out.println(is[0][4]);
            System.out.println(is[0][5]);
            System.out.println(is[0][6]);
            System.out.println(is[0][7]);
            return is[0];

        } catch (Exception ee) {
            ee.printStackTrace();
        }
        return null;
    }
}
